﻿using System;
using System.Web;
using System.Web.UI;
using System.Linq;
using Models;
using System.Web.UI.HtmlControls;
using Newtonsoft.Json.Linq;
using System.Text;
using System.Collections.Generic;

public partial class Admin_store_StoreAdd : AdminBase
{
    public int _Id = 0;
    public string imgArr, user_power="";

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.CacheControl = "no-cache";
        if (!string.IsNullOrEmpty(Request.Params["ID"]))
        {
            _Id = int.Parse(HttpContext.Current.Request.QueryString["ID"]);
        }

        if (!Page.IsPostBack)
        {
           
            if (_Id != 0)
            {
                ShowInfo(_Id);
                //GetMenuString();
            }
        }
    }

    private void ShowInfo(int _id)
    {

        Entities mydb = new Entities();
        {
            S_Store_Press _lt = mydb.S_Store_Press.First(p => p.SID == _id);
            txtSname.Text = _lt.Sname;
            txtSstime.Text = string.IsNullOrEmpty(_lt.Sstime) ? "": _lt.Sstime.ToString();
            txtSetime.Text = string.IsNullOrEmpty(_lt.Setime) ? "" : _lt.Setime.ToString();
            txtStel.Text = _lt.Stel;
            imgArr = getImg(_lt.Simg);
            txtAddress.Text = _lt.Saddress;//地址
            lonlat.Value = _lt.Slongitude;//经度
            lonlat2.Value = _lt.Slatitude;//纬度
            txtSintroduce.Text= _lt.Sintroduce ;

            apicode.Text = _lt.SGuid;
        }
    }

    protected void GetAddressString()
    {
        List<LeftMenu> menus = LeftMenu.Load();
        StringBuilder sb = new StringBuilder();
        Entities db = new Entities();
        var users = db.User.Where(p => p.Ustate == 1);
        foreach (var user in users)
        {
            sb.AppendFormat("<ul class=\"sec_menu\" menuid=\"{0}\">\r\n", user.UID);
            sb.Append("<li  class=\"li_menu\" style=\"float:left;margin:0 15px;width: 220px;list-style-type: none;\"><input type=checkbox  name=\"user_power\" class=\"checkall checkbox-inline i-checks\"  value=\"");
            sb.Append(user.UID + "\"");
            if (_Id != 0) //
            {
               
                    sb.Append(" checked");
            }
            sb.Append("/>");
            sb.AppendFormat(" {0} </li>\r\n", string.IsNullOrEmpty(user.UTrueName)?user.UNickName:user.UTrueName);
        }
        sb.Append("</ul><br>\r\n");
        ltMasterSetting.Text = sb.ToString();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Entities mydb = new Entities();
        {
            S_Store_Press Lt = new S_Store_Press();
            if (_Id != 0)
                Lt = mydb.S_Store_Press.First(p => p.SID == _Id);

            Lt.Sname = txtSname.Text.Trim();
            Lt.Sstime = txtSstime.Text;
            Lt.Setime = txtSetime.Text.ToString();
            Lt.Stel = txtStel.Text.Trim();
            Lt.Simg = xsimg.Value;
            Lt.Saddress = txtAddress.Text.Trim();//地址
            Lt.Slongitude = lonlat.Value;//经度
            Lt.Slatitude = lonlat2.Value;//纬度
            Lt.Stime = DateTime.Now;
            Lt.SGuid= apicode.Text.Trim();//地址

            Lt.Sintroduce = txtSintroduce.Text;
            if (Lt.IsLock != true)
                Lt.IsLock = false;

            //if (Request.Form["user_power"] != null)
            //    user_power = "," + Request.Form["user_power"].ToString() + ",";

            if (_Id == 0)
            {
                mydb.S_Store_Press.Add(Lt);
                mydb.SaveChanges();
                Response.Write("<script> var Close=1;</script>");
            }
            else
            {
                mydb.SaveChanges();
                Response.Write("<script> var Close=1;</script>");
            }
        }
    }
}

